/**
 * @author Gabriel Tuma, Sebastian Demler
 * 
 * This is the Core / Controller Class.
 * Contains the functions
 * 
 */


package com.android.datecalculator;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.GregorianCalendar;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;


public class Core {
	
	private final String DEBUG_TAG = "Debug_DC";
	private GregorianCalendar gcalendar;
	
	/**
	 * Constructor
	 */
	public Core() {
		gcalendar = new GregorianCalendar();
	}
	
	
	
	/* 
	 * ***********************************************************************
	 * 							Public Methodes
	 * ***********************************************************************
	 */
	
	/**
	 * Returns the difference between start and end
	 * @param start
	 * @param end
	 * @param difference_unit
	 * @return
	 */
	public long calcDiff(long start, long end, DifferenceUnits difference_unit){
		if(difference_unit == DifferenceUnits.HOURS)
			return Math.round(Math.abs((start-end))/(1000*60*60));
		
		return Math.round(Math.abs((start-end))/(1000*60*60*24));
	}
	
	/**
	 * Converts the given date parameters in Milliseconds
	 * @param year
	 * @param monthOfYear
	 * @param dayOfMonth
	 * @return
	 */
	public long getDateMillis(int year, int monthOfYear, int dayOfMonth) {
        gcalendar.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
        return gcalendar.getTimeInMillis();
	}
	
	/**
	 * Try to read the Options.
	 * @param context 
	 * @param option_file_name
	 * @return
	 */
	public DCOptions readDCOptions(Context context, String option_file_name) throws FileNotFoundException, StreamCorruptedException,IOException,ClassNotFoundException{
		FileInputStream fis = context.openFileInput(option_file_name);
		ObjectInputStream ois = new ObjectInputStream(fis);
		DCOptions tmp_options = (DCOptions) ois.readObject();
		fis.close();
		ois.close();
		return tmp_options;
	}
	
	
	/**
	 * Try to Write/Save the DCOptions-Object to the given file
	 * 
	 * @param context
	 * @param options
	 * @param option_file_name
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void writeDCOptions(Context context, DCOptions options, String option_file_name) throws FileNotFoundException,IOException {
		FileOutputStream fos = context.openFileOutput(option_file_name, Context.MODE_PRIVATE);
		ObjectOutputStream obs = new ObjectOutputStream(fos);
		obs.writeObject(options);
		fos.close();
		obs.close();
		
	}
	
	/**
	 * Handle the Exception<br>
	 * To show a Toast-notification, set the notice-Parameter<br>
	 * Otherwise it will be sets the default text
	 * 
	 * @param ex
	 * @param context
	 * @param notice
	 * @param debug
	 * @param toast
	 */
	public void catched_exception(Exception ex, Context context, String notice,boolean debug, boolean toast){
		// set the default text if not sets
		if(__String_isNullOrEmpty(notice))
			notice = context.getString(R.string.error_notice_default);
		
		if(toast)
			Toast.makeText(context, notice, Toast.LENGTH_LONG).show();
		
		if(debug && ex != null) {
			String text = ex.getClass().getName() + ": " + ex.getMessage();
			Log.d(DEBUG_TAG, text);
		}
			
	}
	
	
	
	
	/* 
	 * ***********************************************************************
	 * 							Private Methodes
	 * ***********************************************************************
	 */
	
	private boolean __String_isNullOrEmpty(String str){
		if(str == null)
			return true;
		if(str.length() == 0)
			return true;
		if("".compareToIgnoreCase(str.trim()) == 0)
			return  true;
		return false; 
	}

}
